package com.example.album;

import java.util.HashMap;
import java.util.Optional;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class InteractionService {
    @Autowired
    private LikeRepository likeRepository;
    @Autowired
    private CollectionRepository collectionRepository;
    @Autowired
    private CommentRepository commentRepository;
    @Autowired
    private UserRepository userRepository;
    public HashMap<String, Object> getLikeStatus(String albumId, String userId) {
        HashMap<String, Object> result = new HashMap<>();
        Like like = likeRepository.findByUserIdAndAlbumId(userId, albumId);
        boolean liked = (like != null);
        result.put("liked", liked);
        return result;
    }

    public HashMap<String, Object> changeLikeStatus(String albumId, String userId) {
        HashMap<String, Object> result = new HashMap<>();
        Like like = likeRepository.findByUserIdAndAlbumId(userId, albumId);
        if (like == null) {
            like = new Like();
            like.setAlbumId(albumId);
            like.setUserId(userId);
            likeRepository.save(like);
            result.put("liked", true);
        } else {
            likeRepository.delete(like);
            result.put("liked", false);
        }
        return result;
    }

    public HashMap<String, Object> getCollection(String albumId, String userId) {
        HashMap<String, Object> result = new HashMap<>();
        Collection collection = collectionRepository.findByUserIdAndAlbumId(userId, albumId);
        boolean collected = (collection != null);
        result.put("collected", collected);
        return result;
    }
    public HashMap<String, Object> changeCollection(String albumId, String userId) {
        HashMap<String, Object> result = new HashMap<>();
        Collection collection = collectionRepository.findByUserIdAndAlbumId(userId, albumId);
        if (collection == null) {
            collection = new Collection();
            collection.setAlbumId(albumId);
            collection.setUserId(userId);
            collectionRepository.save(collection);
            result.put("collected", true);
        } else {
            collectionRepository.delete(collection);
            result.put("collected", false);
        }
        return result;
    }

    public HashMap<String, Object> getComments(String albumId) {
        HashMap<String, Object> result = new HashMap<>();
        java.util.List<Comment> commentList = commentRepository.findByAlbumId(albumId);
        java.util.List<java.util.Map<String, Object>> comments = new java.util.ArrayList<>();
        for (Comment c : commentList) {
            java.util.Map<String, Object> map = new java.util.HashMap<>();
            //map.put("user", c.getUserId());
            // 获取用户名
            User user = userRepository.findById(c.getUserId()).orElse(null);
            String username = (user != null) ? user.getUsername() : "未知用户";
            map.put("id", c.getCommentId());
            map.put("user", username);
            map.put("text", c.getContent());
            // 直接输出标准时间字符串
            map.put("ts", c.getCreatedAt() != null ? c.getCreatedAt().toString() : "");
            comments.add(map);
        }
        result.put("comments", comments);
        return result;
    }

    public HashMap<String, Object> addComment(String albumId, String userId, String text) {
        HashMap<String, Object> result = new HashMap<>();
        Comment comment = new Comment();
        comment.setCommentId(UUID.randomUUID().toString().replace("-", "").substring(0, 16));
        comment.setAlbumId(albumId);
        comment.setUserId(userId);
        comment.setContent(text);
        comment.setCreatedAt(java.time.LocalDateTime.now());
        commentRepository.save(comment);
        result.put("success", true);
        return result;
    }

    public HashMap<String, Object> deleteComment(String commentId) {
        HashMap<String, Object> result = new HashMap<>();
        commentRepository.deleteById(commentId);
        result.put("success", true);
        return result;
    }
}
